//
//  XYTest.m
//  字符串
//
//  Created by qianfeng on 15/8/16.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "XYTest.h"

@implementation XYTest

//比如输入 @"abcdef"输出@"fedcba"
+(NSString*)reveserString:(NSString*)srcStr{
    NSMutableString * str = [[NSMutableString alloc] init];
    for (int i = 0; i < srcStr.length; i++) {
        unichar ch = [srcStr characterAtIndex:i];
        [str insertString:[NSString stringWithFormat:@"%c",ch] atIndex:0];
    }
    return  str;
}

//比如输入@"aaabbbcceaa" 输出@"3a3b2c1e2a"
+(NSString *)compString:(NSString *)srcStr{
    NSMutableString * str = [[NSMutableString alloc] init];
    int count = 1;
    int flag = 0;
    for (int i = 0;  i < srcStr.length - 1; i++) {
        unichar ch_i = [srcStr characterAtIndex:i];
        for (int j = i + 1; j < srcStr.length; j++) {
            unichar ch_j = [srcStr characterAtIndex:j];
            if (ch_i != ch_j) {
                i = j - 1;
                flag = 1;
                break;
            }
            count++;
            
        }
        [str  appendFormat:@"%d%c",count,ch_i];
        count = 1;
    }
    return str;
}

//比如输入@"aczabczab"  输出@"aaabbcczz"
+(NSString*)sortString:(NSString*)srcStr{
    NSMutableString * str = [[NSMutableString alloc] initWithString:srcStr];
    for (int i = 0; i < srcStr.length - 1; i++) {
        
        for (int j = i + 1;j < srcStr.length ; j++) {
            unichar ch1 = [str characterAtIndex:i];
            unichar ch2 = [str characterAtIndex:j];
            if (ch1 > ch2) {
                [str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch2]];
                [str replaceCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%c",ch1]];
            }
        }
        
    }
    
    return str;
}


//比如输入 @"aBCdEfGA" 输出 @"AbcDeFga"
+(NSString *)reveserChacter:(NSString*)srcStr{
    NSMutableString * str = [[NSMutableString alloc] initWithString:srcStr];
    for (int i = 0; i < srcStr.length ; i++) {
        unichar ch = [str characterAtIndex:i];
        if (islower(ch)) {
            [str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch - 32]];
        }
        if (isupper(ch)) {
            [str replaceCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%c",ch + 32]];
        }
    }
    return str;
}


//5.替换字符串srcStr中从startKey到endKey之间的字符串//返回结果
//比如输入srcStr=@"abcdaabbccdef" startKey=@"da" endKey=@"cde" 输出 @"abbc"
+(NSString *)replaceString:(NSMutableString *)srcStr betweenStartKey:(NSString* )startKey  andEndKey:(NSString*)endKey{

    
    NSRange range1 = [srcStr rangeOfString:startKey];
    NSRange range2 = [srcStr rangeOfString:endKey];
    NSRange range = NSMakeRange(range1.location + range1.length , range2.location - range1.location - range1.length);
    NSString * str = [srcStr substringWithRange:range];

    return  str;
}


@end
